{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 张量的运算\n",
    "- 张量与张量的运算\n",
    "  - 加 减 数乘 点乘 数除 逆、伪逆\n",
    "  - 转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.random.randn(3,4)\n",
    "b = np.random.randn(3,4) \n",
    "#b = np.random.randn(3,1) #使用了广播 \n",
    "\n",
    "a+b,a-b,a*b,a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.random.randn(3,4)\n",
    "b = np.random.randn(4,5)\n",
    "\n",
    "c = a@b \n",
    "c.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.random.randn(2,7,3,4)\n",
    "b = np.random.randn(2,7,4,5)\n",
    "#b = np.random.randn(4,5) #广播\n",
    "\n",
    "\n",
    "c = a@b\n",
    "c.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.random.randn(2,3,4)\n",
    "np.linalg.pinv(a)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a  = np.random.randn(3,4)\n",
    "\n",
    "a = a.transpose()\n",
    "a = a.T\n",
    "a.shape\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.random.randn(3,4,5)\n",
    "a = a.transpose(0,2,1)\n",
    "a.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 形状变换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.random.randn(3,6,5)\n",
    "\n",
    "a = a.reshape(3,3,2,5)\n",
    "print(a.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = a.reshape(9,10)\n",
    "print(a.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install pillow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 图片\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import numpy as np\n",
    "\n",
    "img = Image.open(\"../1.jpg\")\n",
    "# img.show()\n",
    "data = np.array(img)\n",
    "print(data.shape)\n",
    "\n",
    "# data = data.reshape(2,640,2,360,3)\n",
    "# data = data.transpose(0,2,1,3,4)\n",
    "# data = data.reshape(4,640,360,3)\n",
    "\n",
    "data = data.reshape(640,2,360,2,3)\n",
    "data = data.transpose(1,3,0,2,4)\n",
    "data = data.reshape(4,640,360,3)\n",
    "\n",
    "img = Image.fromarray(data[0])\n",
    "img.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import numpy as np\n",
    "\n",
    "img = Image.open(\"../1.jpg\")\n",
    "# img.show()\n",
    "data = np.array(img)\n",
    "data = data.transpose(1,0,2)\n",
    "\n",
    "print(data.shape)\n",
    "\n",
    "img = Image.fromarray(data)\n",
    "img.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import numpy as np\n",
    "\n",
    "img = Image.open(\"../1.jpg\")\n",
    "# img.show()\n",
    "data = np.array(img)\n",
    "data = data[:,::-1]\n",
    "\n",
    "print(data.shape)\n",
    "\n",
    "img = Image.fromarray(data)\n",
    "img.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 30)\n"
     ]
    }
   ],
   "source": [
    "a = np.random.randn(3,6,5)\n",
    "a = a.reshape(3,-1)\n",
    "\n",
    "print(a.shape)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
